<?php

namespace App\Admin\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use \App\Models\Brand;
use \App\Models\BrandModel;
use Illuminate\Support\Facades\Storage;

class BrandModelsController extends AdminController
{
    protected $title = '品牌车型管理';
    
    protected function grid()
    {
        $grid = new Grid(new BrandModel());

	    $grid->disableExport();
	
	    $grid->column('id', 'ID');

	     $grid->column('brand_id', '品牌')->display(function () {
	    	return $this->brand ? $this->brand->name : '';
	    });
	     $grid->column('image', '车型logo')->display(function ($value) {
	        if (!Storage::disk('oss')->exists($value)) {

		       // $this->update(['cover' => null]);
		        return '';
	        } else {
		        return _grid_image($value);
	        }
        });
	    $grid->column('name', '名称');
	    
	    $grid->column('created_at', '创建时间')->display(function () {
		    return $this->created_at->toDatetimeString();
	    });
		$this->gridFilter($grid);
	    $grid->disableRowSelector();

        return $grid;
    }

     protected function gridFilter(Grid $grid)
    {
	    $grid->filter(function (Grid\Filter $filter) {
		    $filter->expand();

		    $filter->column(1/2, function (Grid\Filter $filter) {
		    	$filter->disableIdFilter();
			    $filter->like('name', '标题查询');

		    });

		   
	    });
    }
	
	protected function detail($id)
	{
		$show = new Show(BrandModel::findOrFail($id));
		
		$show->field('id', __('Id'));
		$show->field('name', __('Name'));
		$show->field('image', __('Image'));
		$show->field('created_at', __('Created at'));
		$show->field('updated_at', __('Updated at'));
		
		return $show;
	}
    
    protected function form()
    {
        $form = new Form(new BrandModel());

        $form->select('brand_id','品牌')->options(Brand::all()->pluck('name', 'id'));
	    $form->text('name', '车型名称');
	    $form->image('image', '车型logo')->uniqueName()->removable();

        return $form;
    }
}
